<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/9
 * Time: 11:59 AM
 */

namespace app\index\service;
use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\common\service\DownService;
use think\Db;
use think\Exception;

class ShopwarehousestockService
{
    static public function getList($param, $is_download = false)
    {
        try{
            $where = "where 1 = 1 ";
            $pageNumber = isset($param['page']) ? $param['page'] : 1;
            $pageData = isset($param['limit']) ? $param['limit'] : 20;
            $warehouse_shop_permission = ConfigBase::getWarehouseShopPermission();  // 仓库权限
            $id = isset($param['id']) ? $param['id'] : '';  // ID
            $base_warehouse_id = isset($param['base_warehouse_id']) ? $param['base_warehouse_id'] : '';  // 收货仓库ID
            $sku = isset($param['sku']) ? $param['sku'] : '';  // SKU
            $stock_age_s = isset($param['stock_age_s']) ? $param['stock_age_s'] : '';  // 库龄开始
            $stock_age_e = isset($param['stock_age_e']) ? $param['stock_age_e'] : '';  // 库龄结束
            $sku_status = isset($param['sku_status']) ? $param['sku_status'] : '';  // 状态
            $offset = ($pageNumber - 1) * $pageData;
            $limit = " LIMIT $offset,$pageData";

            if (!empty($id)) {
                $id = trim($id);
                $where .= " AND id = $id";
            }
            if (!empty($base_warehouse_id)) {
                $base_warehouse_id = trim($base_warehouse_id);
                $where .= " AND base_warehouse_id = $base_warehouse_id";
            }
            if (!empty($sku)) {
                $sku = trim($sku);
                $where .= " AND sku = '{$sku}'";
            }
            if (!empty($stock_age_s)) {
                $stock_age_s = trim($stock_age_s);
                $where .= " AND stock_age >= '{$stock_age_s}'";
            }
            if (!empty($stock_age_e)) {
                $stock_age_e = trim($stock_age_e);
                $where .= " AND stock_age <= '{$stock_age_e}'";
            }
            if (!empty($sku_status)) {
                $where .= " AND sku_status = {$sku_status}";
            }
            if (!empty($warehouse_shop_permission)) {
                $warehouse_shop_permission = trim($warehouse_shop_permission);
                $where .= " AND base_warehouse_id in ($warehouse_shop_permission)";
            }else{
                $where .= " AND base_warehouse_id in ('')";
            }

            $sqlCount = "SELECT COUNT(id) num FROM shop_warehouse_stock {$where}";
            $countArr = ToolModel::queryData($sqlCount);
            if (empty($countArr) || $countArr[0]['num'] == 0) {
                if ($is_download) exitJson(["code" => "500", "msg" => "无有效数据"]);
                return array('total' => 0, 'list' => array());
            }
            $fileds = 'id,base_warehouse_id,base_warehouse_name,sku,sku_name,physical_stock,cost,wam as price,
                      stock_age,update_time,create_time,sku_status,case sku_status when 1 then "新产品" 
                      when 2 then "清仓品" when 3 then "在售品" when 4 then "下架品" end sku_status_name';

            $sql = "SELECT {$fileds} FROM shop_warehouse_stock {$where} ORDER BY id DESC {$limit}";
            if ($is_download) return "SELECT {$fileds} FROM shop_warehouse_stock {$where} ORDER BY id DESC ";
            $list = ToolModel::queryData($sql);
            $data = array();
            $data['total'] = intval($countArr[0]['num']);
            $data['list'] = $list;

            return $data;
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }



    /**
     * 导出数据
     * @param $param
     * @return array
     * @author duanwenjie
     */
    static public function export($param)
    {
        $sql = self::getList($param, true);
        $title = array(
            'base_warehouse_name' => '仓库',
            'sku' => 'SKU',
            'sku_name' => 'SKU名称',
            'sku_status_name' => 'SKU状态',
            'physical_stock' => '实物库存',
            'cost' => '库存成本',
            'price' => '加权单价',
            'stock_age' => '库龄'
        );
        $res = DownService::getData($sql, $title, '', $param);

        return $res;
    }

}